<template>
	<view>
		<view class="container">
			<view class="title">登录</view>
			<view class="info">
				<view class="center">
				
					<input placeholder="请输入用户名" class="info-input" v-model="username" />
					<br></br>
					
					<input placeholder="请输入密码" class="info-input" v-model="password" type="password" />
				</view>
				<view class="info-bottom">
					<!-- <view @click="loginByCode">验证码登录</view> -->
					<view class="info_right">
						<!-- <view @click="findPassword">忘记密码？</view> -->
						<!-- <view @click="regist">注册</view> -->
					</view>

				</view>
				<view class="login">
					<button @click='login'>登录</button>
				</view>
			</view>
			<!-- <view class="fotter">
				<tui-divider width="80%">其他方式登录</tui-divider>
				<view class="other-login">
					<view class="other-item">
						<view class="icon">
							<image src="../../static/images/share/icon_wechat.png" data-loginType="weixin"
								@click="otherLogin" mode="aspectFill"></image>
						</view>
						<view class="font">微信</view>
					</view>
				</view>
			</view> -->
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				username: "",
				password: ""
			};
		},
		mounted() {
			// this.getUsrByUserName();
		},
		methods: {
			login() {
				uni.request({
					url: 'http://localhost:8081/usr/userlogin/'+this.username+'/'+this.password,
					method: 'GET',
					data: {
						username: this.username,
						password: this.password
					},
					success: (res) => {
						if (res.data === '登录成功') {
							uni.showToast({
								title: '登录成功',
								duration: 2000
							});
							// 登录成功后的操作
							this.getUsrByUserName(this.username);
							
						} else {
							uni.showToast({
								title: '登录失败：' + res.data,
								duration: 2000,
								icon: 'none'
							});
						}
					},
					fail: (errMsg) => {
						uni.showToast({
							title: '请求失败，请稍后再试',
							duration: 2000,
							icon: 'none'
						});
						console.error(errMsg);
					}
				});
			},
			getUsrByUserName(username) {
				uni.request({
					url: 'http://localhost:8081/usr/getUsrByUserName/' + username,
					method: 'GET',
					header: {
						'content-type': 'application/json'
					},
					success: (res) => {
						if (res.statusCode === 200) {
							this.usr = res.data;
							uni.setStorageSync('username', username)
							uni.switchTab({
							  url: "/pages/person/person"
							});
						}
					},
					fail: (err) => {
						console.error(err);
					}
				});
			},
			
			regist() {
				uni.navigateTo({
					url: "../../pages/regist/regist"
				})
			},
			otherLogin() {
				uni.getUserProfile({
					desc: '用于您的页面展示用户头像与昵称',
					lang: 'zh_CN',
					success: res => {
						console.log(res);
						this.isShowLogin = false;
						this.userInfo = res.userInfo;
					},
					fail: err => {
						console.log(err.errMsg);
					}
				});
			}

		}
	}
</script>

<style lang="scss">
	@import url(login.css);
</style>